package com.example.guitartensioncalculator;

import guitarTensionCalc.GuitarTensionCalc;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;

public class MainActivity extends Activity {
	final ArrayList<GuitarTensionCalc> stringArray = new ArrayList<GuitarTensionCalc>();
	final CustomGuitarAdapter adapter = new CustomGuitarAdapter(stringArray,this);
	boolean imperial = true;
	final Context parentContext = this;
	
	double scaleLength = 26.5;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
		setContentView(R.layout.activity_main);
		final Intent intent = new Intent(MainActivity.this, StringInformationActivity.class);
		final Intent intentChangeString = new Intent(MainActivity.this, StringInformationActivity.class);


		// Getting listview from xml
		ListView stringList = (ListView) findViewById(R.id.GuitarStringList);
		 
		// *Temporary* Create a Button at the bottom to add more strings
		Button addStringButton = new Button(this);
		addStringButton.setText("+Add String");
		stringList.addFooterView(addStringButton);
		
		addStringButton.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){
			startActivityForResult(intent, 1);
			Log.i("button", "Click");
			
		}});
	
		
		
		LayoutInflater inflater = getLayoutInflater();
		ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listview_header, stringList, false);
		stringList.addHeaderView(header, null, false);
		
		final Button setScale_1 = (Button)findViewById(R.id.button1);
		Button setScale_2 = (Button)findViewById(R.id.button2);
		
		
		setScale_1.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){			
			final Dialog dialog = new Dialog(parentContext);
			dialog.setContentView(R.layout.scale_length);
			dialog.setTitle("Set Scale Length");
 
			// set the custom dialog components - text, image and button
			final EditText editTextScale = (EditText) dialog.findViewById(R.id.editTextScale);
			editTextScale.setText(Double.toString(scaleLength));
			final Switch switchScale = (Switch)dialog.findViewById(R.id.switch1);
			switchScale.setChecked(imperial);
			Button buttonSubmit = (Button)dialog.findViewById(R.id.button2);

			buttonSubmit.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					String temp1 = editTextScale.getText().toString();
					if(!temp1.matches(""))
					{
					boolean temp2 = switchScale.isChecked();
					scaleLength = Double.parseDouble(temp1);
					imperial = temp2;
					setScale_1.setText("Scale Length : " + temp1 + (imperial?"in":"cm"));
					updateStringScale();	//Resets the scale lenght on all strings
					}
					dialog.dismiss();		
				}
			});
			dialog.show();
		}});
		
		setScale_2.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){
			Log.i("header", "Click2");
			
			final Dialog dialog = new Dialog(parentContext);
			dialog.setContentView(R.layout.scale_length);
			dialog.setTitle("Set Scale Length");
 
			// set the custom dialog components - text, image and button
			final EditText editTextScale = (EditText) dialog.findViewById(R.id.editTextScale);
			editTextScale.setText(Double.toString(scaleLength));
			final Switch switchScale = (Switch)dialog.findViewById(R.id.switch1);
			switchScale.setChecked(imperial);
			Button buttonSubmit = (Button)dialog.findViewById(R.id.button2);

			buttonSubmit.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					String temp1 = editTextScale.getText().toString();
					boolean temp2 = switchScale.isChecked();
					if(!temp1.matches(""))
					{
					scaleLength = Double.parseDouble(temp1);
					imperial = temp2;
					setScale_1.setText("Scale Length : " + temp1 + (imperial?"in":"cm"));
					updateStringScale();	//Resets the scale lenght on all strings
					}
					dialog.dismiss();
				}
			});
			dialog.show();		
		}});
		
		stringList.setAdapter(adapter);
	    
	    //Called when the user clicks on a string in the StringList
	    stringList.setOnItemClickListener(new OnItemClickListener()
	    {
	    public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
	    {

    		Log.i("adapter size", Integer.toString(position));
    		GuitarTensionCalc tempVariable =  stringArray.get(position-1);
    		Double tempGauge = tempVariable.gauge;
    		int tempOctave = tempVariable.octave;
    		int tempStringIndex = tempVariable.strTypeNum;
    		int tempNoteIndex = tempVariable.noteidx;
    		intentChangeString.putExtra("userGauge", tempGauge);
    		intentChangeString.putExtra("userOctave", tempOctave);
    		intentChangeString.putExtra("userString", tempStringIndex);
    		intentChangeString.putExtra("userNote", tempNoteIndex);
    		
    		startActivity(intentChangeString);
	        }
	    });
	    }
	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    // TODO Auto-generated method stub
	    super.onActivityResult(requestCode, resultCode, data);
	    Intent intent = new Intent();
	    if (requestCode == 1){
    		Log.i("Activity", "Returned From Activity:1");
	    	if(data == null){
	    		Log.i("Add String", "Data bundle was empty");
	    		return;
	    	}
	    int octave = data.getIntExtra("octave", -1);
	    int note = data.getIntExtra("note", -1);
	    Double gauge = data.getDoubleExtra("gauge", -1);
	    int strType = data.getIntExtra("StrType", -1);


		GuitarTensionCalc temp = new GuitarTensionCalc();
		temp.octave = octave;
		temp.gauge = gauge;
		temp.strType = strType;
		temp.setScaleLength(scaleLength);
		
		temp.setFreqVars(note);
		temp.calculateFreq();
		temp.setUnitWeight(strType, gauge);
				
		stringArray.add(temp);
		adapter.notifyDataSetChanged();

	    
	    	
	    }
	}
	
	// Call when the user changes the guitar scale length to update the strings
	public void onScaleChange(){
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	//Updates each of the string when the user changes the ScaleLength
	public void updateStringScale(){
		int stringSize = stringArray.size();
		for(int count = 0; count < stringSize; count++)
		{
			GuitarTensionCalc temp = stringArray.get(count);
			temp.setScaleLength(scaleLength);
			temp.cm = !imperial;
			temp.calculateTension();
		}
		adapter.notifyDataSetChanged();	
	}
	

	
	
		
	
	

}